<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>动态详情 - 测盟汇系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        :root {
            --primary-color: #2c5545;
            --primary-light: #3a7d62;
            --primary-hover: #1e4636;
            --accent-color: #8ba888;
            --dynamic-color: #cce622;
            --text-color: #333;
            --text-light: #555;
            --border-color: #d1d9d6;
            --bg-light: #f5f7f6;
            --bg-page: #e8ecea;
            --white: #fff;
            --shadow: 0 4px 24px rgba(44, 85, 69, 0.1);
            --radius: 12px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: var(--bg-page);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--text-color);
            line-height: 1.6;
        }

        .header {
            background: var(--primary-color);
            color: var(--white);
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .logo {
            display: flex;
            align-items: center;
        }

        .logo i {
            font-size: 1.8rem;
            margin-right: 0.8rem;
        }

        .logo h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .user-info i {
            font-size: 1.2rem;
            margin-right: 0.5rem;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: var(--white);
            padding: 0.5rem 1rem;
            border-radius: 4px;
            cursor: pointer;
            transition: var(--transition);
            margin-left: 1rem;
            display: flex;
            align-items: center;
        }

        .logout-btn i {
            margin-right: 0.5rem;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .content-card {
            background: var(--white);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 2rem;
            margin-bottom: 2rem;
            border: 1px solid var(--border-color);
        }

        .page-title {
            color: var(--primary-color);
            font-size: 1.8rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
        }

        .page-title i {
            margin-right: 1rem;
        }

        .btn {
            padding: 0.8rem 1.5rem;
            border-radius: var(--radius);
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
        }

        .btn i {
            margin-right: 0.5rem;
        }

        .btn-secondary {
            background: var(--bg-light);
            color: var(--text-color);
            border: 1px solid var(--border-color);
        }

        .btn-secondary:hover {
            background: #e1e8e4;
            transform: translateY(-2px);
        }

        .detail-header {
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 1rem;
            margin-bottom: 1.5rem;
        }

        .detail-title {
            color: var(--primary-color);
            font-weight: 700;
            margin-bottom: 1.5rem;
        }

        .meta-info {
            display: flex;
            flex-wrap: wrap;
            gap: 1.5rem;
            margin-bottom: 1.5rem;
            color: var(--text-light);
        }

        .meta-info span {
            font-weight: 600;
            display: flex;
            align-items: center;
        }

        .meta-info i {
            margin-right: 0.5rem;
            color: var(--primary-color);
        }

        .cover-image {
            max-width: 100%;
            height: auto;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            margin: 1.5rem 0;
            transition: var(--transition);
        }

        .cover-image:hover {
            transform: scale(1.02);
        }

        .section-title {
            color: var(--primary-color);
            font-weight: 600;
            margin: 1.5rem 0 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            align-items: center;
        }

        .section-title i {
            margin-right: 0.8rem;
        }

        .content-area {
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            padding: 1.5rem;
            margin-top: 1rem;
            background: var(--bg-light);
        }

        .audit-card {
            background: var(--white);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 2rem;
            margin-top: 2rem;
            border-left: 0.25rem solid var(--primary-color);
        }

        .audit-title {
            color: var(--primary-color);
            font-weight: 600;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
        }

        .audit-title i {
            margin-right: 0.8rem;
        }

        .form-control {
            border-radius: var(--radius);
            padding: 0.75rem;
            border: 1px solid var(--border-color);
            background: var(--bg-light);
            transition: var(--transition);
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(44, 85, 69, 0.25);
            background: var(--white);
        }

        .btn-success {
            background: var(--primary-light);
            border-color: var(--primary-light);
            color: var(--white);
        }

        .btn-success:hover {
            background: var(--primary-hover);
            transform: translateY(-2px);
        }

        .btn-danger {
            background: #e74a3b;
            border-color: #e74a3b;
            color: var(--white);
        }

        .btn-danger:hover {
            background: #c0392b;
            transform: translateY(-2px);
        }

        .status-tag {
            padding: 0.4rem 0.8rem;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 500;
            display: inline-flex;
            align-items: center;
        }

        .status-tag i {
            margin-right: 0.3rem;
            font-size: 0.8rem;
        }

        .status-pending {
            background: #fff3cd;
            color: #856404;
        }

        .status-published {
            background: #d4edda;
            color: #155724;
        }

        .action-buttons {
            display: flex;
            gap: 1rem;
            margin-top: 1.5rem;
        }

        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                text-align: center;
                padding: 1rem;
            }

            .user-info {
                margin-top: 1rem;
            }

            .meta-info {
                flex-direction: column;
                gap: 0.8rem;
            }

            .action-buttons {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <div class="logo">
        <i class="fas fa-store"></i>
        <h1>测盟汇系统</h1>
    </div>
    <div class="user-info">
        <i class="fas fa-user-shield"></i>
        <span>超级管理员：<span th:text="${session.user.username}"></span></span>
        <a th:href="@{/user/logout}" class="logout-btn">
            <i class="fas fa-sign-out-alt"></i>
            退出登录
        </a>
    </div>
</div>

<div class="container">
    <a href="/admin_list" class="btn btn-secondary">
        <i class="fas fa-arrow-left"></i>返回列表
    </a>

    <div class="content-card">
        <div class="detail-header">
            <h1 class="detail-title" id="title"></h1>
            <div class="meta-info">
                <span><i class="fas fa-user"></i><strong>作者：</strong><span id="author"></span></span>
                <span><i class="fas fa-clock"></i><strong>发布时间：</strong><span id="publishTime"></span></span>
                <span><i class="fas fa-info-circle"></i><strong>状态：</strong><span id="status"></span></span>
            </div>
        </div>

        <img id="cover" class="cover-image" src="" alt="封面图" onerror="this.src='https://via.placeholder.com/800x450?text=封面图片加载失败'">

        <h3 class="section-title"><i class="fas fa-paragraph"></i>简介</h3>
        <p id="introduction" class="lead"></p>

        <h3 class="section-title"><i class="fas fa-file-text"></i>内容</h3>
        <div id="content" class="content-area"></div>
    </div>

    <!-- 审核区域（仅待审核状态显示） -->
    <div class="audit-card" id="auditArea">
        <h3 class="audit-title"><i class="fas fa-clipboard-check"></i>审核操作</h3>
        <div class="mb-3">
            <label for="auditReason" class="form-label">审核意见（可选）</label>
            <textarea class="form-control" id="auditReason" rows="3" placeholder="请输入审核意见..."></textarea>
        </div>
        <div class="action-buttons">
            <button class="btn btn-success" onclick="audit(1)">
                <i class="fas fa-check-circle"></i>通过
            </button>
            <button class="btn btn-danger" onclick="audit(0)">
                <i class="fas fa-times-circle"></i>驳回
            </button>
        </div>
    </div>
</div>

<script>
    let dynamicId = null;

    // 页面加载时获取详情
    $(document).ready(function() {
        const urlParams = new URLSearchParams(window.location.search);
        dynamicId = urlParams.get('id');
        if (!dynamicId) {
            alert('参数错误，未找到动态ID');
            window.location.href = '/admin_list';
            return;
        }
        loadDynamicDetail();
    });

    // 加载动态详情
    function loadDynamicDetail() {
        $.ajax({
            url: '/dynamic/detail',
            method: 'GET',
            data: { id: dynamicId },
            success: function(response) {
                if (response.code === 200) {
                    renderDetail(response.data);
                } else {
                    alert('加载失败：' + response.message);
                    window.location.href = '/admin_list';
                }
            },
            error: function() {
                alert('服务器异常，请稍后重试');
            }
        });
    }

    // 渲染详情
    function renderDetail(dynamic) {
        $('#title').text(dynamic.title);
        $('#author').text(dynamic.author || '未知');
        $('#publishTime').text(formatDate(dynamic.publishTime));
        $('#introduction').text(dynamic.introduction);
        $('#content').html(dynamic.content);
        $('#cover').attr('src', dynamic.coverUrl || 'https://via.placeholder.com/800x450?text=暂无封面');

        // 状态显示
        let statusText = '';
        if (dynamic.status === 0) {
            statusText = '<span class="status-tag status-pending"><i class="fas fa-hourglass-half"></i>待审核</span>';
            $('#auditArea').show(); // 待审核状态显示审核区域
        } else {
            statusText = '<span class="status-tag status-published"><i class="fas fa-check-circle"></i>已发布</span>';
            $('#auditArea').hide(); // 已发布状态隐藏审核区域
        }
        $('#status').html(statusText);
    }

    // 审核操作（1=通过，0=驳回）
    function audit(pass) {
        const reason = $('#auditReason').val().trim();
        if (pass === 0 && !confirm('确定要驳回这条动态吗？')) {
            return;
        }
        if (pass === 1 && !confirm('确定要通过这条动态吗？')) {
            return;
        }

        $.ajax({
            url: '/api/admin/audit',
            method: 'POST',
            data: {
                dynamicId: dynamicId,
                newStatus: pass, // 1=通过（发布），0=驳回（仍待审核）
                reason: reason
            },
            success: function(response) {
                if (response.code === 200) {
                    alert('审核成功');
                    window.location.href = '/admin_list';
                } else {
                    alert('审核失败：' + response.message);
                }
            },
            error: function() {
                alert('服务器异常，请稍后重试');
            }
        });
    }

    // 格式化日期
    function formatDate(dateStr) {
        const date = new Date(dateStr);
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        });
    }
</script>
</body>
</html>